package com.bedpotato.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bedpotato.constants.ProxyClawerConstants;
import com.bedpotato.http.BasicHttpMethod;
import com.bedpotato.mapper.ProxyMapper;
import com.bedpotato.po.ProxyPO;

@Service
public class FunctionTestService {
	Logger logger = Logger.getLogger(this.getClass());
	@Resource
	ProxyMapper proxyMapper;

	public void testFuncion() {
		boolean isDBok = testDB();
		boolean isNetWorkOK = testNetWork();
		if (isDBok && isNetWorkOK) {
			logger.info("测试正常");
		}else {
			logger.info("测试异常");
		}

	}

	private boolean testDB() {
		List<ProxyPO> proxyList = proxyMapper.getAllProxy();
		if (proxyList == null || proxyList.size() == 0) {
			logger.info("数据库异常");
			return false;
		} else {
			logger.info("数据库正常");
			return true;
		}
	}

	private boolean testNetWork() {
		BasicHttpMethod basicHttpMethod = new BasicHttpMethod();
		try {
			String page = basicHttpMethod.get(ProxyClawerConstants.TESTURL);
			if (page != null && page.length() > 10) {
				logger.info("网络正常");
				return true;
			}
		} catch (Exception e) {
		}
		logger.info("网络异常");
		return false;
	}
}
